#!/bin/bash

# 输出文件名
OUTPUT_FILE=${OUTPUT_FILE:-bookmarks.yaml}

# 输出文件名
INPUT_FILE=${INPUT_FILE:-link.json} # /DATA/var/lib/casaos/1/link.json

# 默认主机名
DEFAULT_HOSTNAME=${DEFAULT_HOSTNAME:-op}

# 默认主机名
DEFAULT_CATEGORY=${DEFAULT_CATEGORY:-其他}


function gen_config() {
    projects=$(docker compose ls --format json | jq -r '.[].ConfigFiles')

    echo date:`date`

    # 初始化输出内容
    local output="---\n"
    output+="# For configuration options and examples, please see:\n"
    output+="# https://gethomepage.dev/latest/configs/bookmarks\n"

    declare -A categories # 使用关联数组来存储分类和条目

    IFS=$'\n\n'
    for compose_file in $projects; do
        if [[ "$compose_file" == *":"* ]] || [[ ! -f "$compose_file" ]]; then
            continue
        fi

        echo  "处理文件：$compose_file"

        c_port=$(yq e '.x-casaos.port_map' "$compose_file")
        c_index=$(yq e '.x-casaos.index' "$compose_file")
        c_hostname=$(yq e '.x-casaos.hostname' "$compose_file")
        c_scheme=$(yq e '.x-casaos.scheme' "$compose_file")
        c_icon=$(yq e '.x-casaos.icon' "$compose_file")
        c_category=$(yq e '.x-casaos.category' "$compose_file")
        c_store_app_id=$(yq e '.x-casaos.title.custom' "$compose_file")

        # Fallbacks for store_app_id
        if [ -z "$c_category" ]; then
            c_category=$DEFAULT_CATEGORY
        fi
        if [[ "unknown" == "$c_category" ]]; then
            c_category=$DEFAULT_CATEGORY
        fi
        if [ -z "$c_store_app_id" ]; then
            c_store_app_id=$(yq e '.x-casaos.title.en_us' "$compose_file")
        fi
        if [ -z "$c_store_app_id" ]; then
            c_store_app_id=$(yq e '.x-casaos.title.zh_cn' "$compose_file")
        fi

        if [ -z "$c_store_app_id" ]; then
            c_store_app_id=$(yq e '.x-casaos.store_app_id' "$compose_file")
        fi

        if [[ -z "$c_store_app_id" ]] || [[ "null" == "$c_store_app_id" ]]; then
            continue
        fi

        # Fallbacks for other variables
        if [ -z "$c_port" ]; then
            c_port=""
        else
            c_port=":${c_port}"
        fi
        c_index=${c_index:-"/"}
        if [[ "null" == "$c_index" ]]; then
            c_index=/
        fi
        c_hostname=${c_hostname:-$DEFAULT_HOSTNAME}
        c_scheme=${c_scheme:-"http"}

        c_href="$c_scheme://$c_hostname$c_port$c_index"

        # Collect entries under their categories
        categories["$c_category"]+="
  - ${c_store_app_id}:
      - abbr: ${c_store_app_id:0:2}
        icon: ${c_icon}
        href: ${c_href}
"
    done
    
    if [[ -f "$INPUT_FILE" ]]; then

      SED_STR="-_-_-_"

      link_data=`cat $INPUT_FILE | jq -r '.[] | (.hostname) + "'$SED_STR'" + .name + "'$SED_STR'" + .icon '`

        # 使用IFS将字符串拆分到数组
      
      IFS=$'\n\n'
      for l_data in $link_data; do

        c2_href=$(echo $l_data | awk -F"$SED_STR" '{print $1}')
        c2_name=$(echo $l_data | awk -F"$SED_STR" '{print $2}')
        c2_icon=$(echo $l_data | awk -F"$SED_STR" '{print $3}')
        categories["$DEFAULT_CATEGORY"]+="
  - ${c2_name}:
      - abbr: ${c2_name:0:2}
        icon: ${c2_icon}
        href: ${c2_href}
"
      done

    fi


    # Append each category and its entries to the output
    for category in "${!categories[@]}"; do
        output+="- ${category}:${categories[$category]}\n"
    done

    echo -e "$output" > "$OUTPUT_FILE"
}

gen_config

# /usr/sbin/sshd
# /etc/init.d/sshd start

# 监听 Docker 事件并重新生成配置
docker events --filter event=create | while read -r event; do
    gen_config
done

# docker build . -t jonneywu/casa-to-homepage:5 -f Dockerfile 

# docker run --rm -ti -v portainer-data:/data -v /var/run/docker.sock:/var/run/docker.sock  --entrypoint="" --name test -v `pwd`:`pwd` -w `pwd` jonneywu/casa-to-homepage:5 bash # `pwd`/gen-homepage-config.sh

